#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
struct stu {
	char s[21];
	int y, m, d,sum;
}qwq[101],qwe[1];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%s%d%d%d", qwq[i].s, &qwq[i].y, &qwq[i].m, &qwq[i].d);
		qwq[i].sum = qwq[i].y * 10000 + qwq[i].m * 100 + qwq[i].d;
	}
	for (int i = 0; i < n - 1; i++)
		for (int j = i + 1; j < n; j++)
		{
			if(qwq[i].sum>qwq[j].sum)
			{
				qwe[0] = qwq[i];
				qwq[i] = qwq[j];
				qwq[j] = qwe[0];
			}
		}
	for (int i = 0; i < n; i++) printf("%s\n", qwq[i].s);
	return 0;
}